package bme.andraskindler.treenventory.activity;

import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import bme.andraskindler.treenventory.R;
import bme.andraskindler.treenventory.fragment.ProgressDialogFragment;
import bme.andraskindler.treenventory.model.TreeData;
import bme.andraskindler.treenventory.provider.DetailsPagerAdapter;
import bme.andraskindler.treenventory.provider.RestManager;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.common.collect.Lists;

public class DetailsActivity extends SherlockFragmentActivity {

	private ViewPager mPager;
	private GoogleMap mMap;
	
	private class DownloadTask extends AsyncTask<Void, Void, List<TreeData>> {

		private ProgressDialogFragment fragment;

		@Override
		protected List<TreeData> doInBackground(Void... params) {
			final List<TreeData> list = Lists.newArrayList();
			TreeData[] data = RestManager.getTrees();
			for (TreeData treeData : data) {
				list.add(treeData);
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<TreeData> result) {
			super.onPostExecute(result);
			mPager.setAdapter(new DetailsPagerAdapter(result));
			for (TreeData treeData : result) {
				mMap.addMarker(new MarkerOptions().position(treeData.getLatLng()));
			}

			mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(result.get(0).getLatLng(), 16), 10, null);

			fragment.dismiss();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			fragment = ProgressDialogFragment.newInstance("Kérem, várjon", "Adatok letöltése folyamatban...");
			fragment.show(DetailsActivity.this.getSupportFragmentManager(), ProgressDialogFragment.class.getCanonicalName());
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) != ConnectionResult.SUCCESS) {
			GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this), this, 10, new OnCancelListener() {
				public void onCancel(DialogInterface dialog) {
					finish();
				}
			}).show();
		} else {
			new DownloadTask().execute();
			final View root = getLayoutInflater().inflate(R.layout.activity_details, null);
			setContentView(root);

			mMap = SupportMapFragment.class.cast(getSupportFragmentManager().findFragmentById(R.id.map_details)).getMap();

			mPager = ViewPager.class.cast(root.findViewById(R.id.vp_details));
			mPager.setOnPageChangeListener(new OnPageChangeListener() {

				public void onPageSelected(int arg0) {
					final TreeData treedata = DetailsPagerAdapter.class.cast(mPager.getAdapter()).getItem(arg0);
					mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(treedata.getLatLng(), 16), 500, null);
				}

				public void onPageScrolled(int arg0, float arg1, int arg2) {
				}

				public void onPageScrollStateChanged(int arg0) {
				}
			});
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			break;
		}
		return false;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

}
